package bairui.international.weixin.portal.miniapp.controller.product.product;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.spring.web.controller.enhance.output.format.RestDataOutputFormat;
import com.querydsl.core.BooleanBuilder;

import bairui.international.model.domain.jpa.QTB_Product;
import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductCategory;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_UserAmount;
import bairui.international.model.status.State_Product;
import bairui.international.model.type.Type_ProductProperty;
import bairui.international.service.Service_Product;
import bairui.international.service.Service_ProductCategory;
import bairui.international.service.Service_ProductPropertyItem;
import bairui.international.service.Service_User;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * 列表
 *
 * @generated
 * 
 * @see STest_C_Product_List_Get 测试
 *
 */
@Controller
public class C_Product_List_Get
 extends SC_Product_List_Get 
{
	
	@Autowired private Service_Product service_Product;
	@Autowired private Service_ProductPropertyItem service_ProductPropertyItem;
	private static QTB_Product QRoot = QTB_Product.tB_Product;
	@Autowired private Service_User service_User;
	@Autowired private Service_ProductCategory service_ProductCategory;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@PostMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
			ModelMap map, HttpServletRequest request,
			@RequestParam(required=false) String[] type,
			@RequestBody Input filterInput,
			@RequestParam(required=false) Long townId,
			@RequestParam(required=false) Long wineId,
			@RequestParam(required=false) String name,
			@RequestParam String skey
	) throws Exception {
		// 处理易物积分
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_UserAmount grade = user.getUserAmount();
		Double totalScore = null == grade ? 0D : grade.getGrade();
		// 分类类型
		TB_ProductCategory category = null;
		BooleanBuilder typeQuery = new BooleanBuilder();
		if(null != type){
			typeQuery.and(QRoot.rootCategory.name.in(type));
			category = service_ProductCategory.findOne(it->it.name.in(type).and(it.parent.isNull()));
		}
		// wineId 过滤(为包装提供)
		BooleanBuilder wineQuery = new BooleanBuilder();
		if(null != wineId){
			wineQuery.and(QRoot.wineBottleNormsWine.wraps.any().id.eq(wineId));
		}
		// 过滤属性参数
		BooleanBuilder properyItemFilterQuery = new BooleanBuilder();
		if(null != filterInput){
			for(List<Long> items : filterInput.getFilters()){
				BooleanBuilder properyItemFilterQueryGroup = new BooleanBuilder();
				for(Long item : items){
					TB_ProductPropertyItem pItem = service_ProductPropertyItem.findById(item);
					Type_ProductProperty pType = pItem.getProductProperty().getType();
					if(pType == Type_ProductProperty.Fix){
						properyItemFilterQueryGroup.or(QRoot.productPropertyItems.contains(new TB_ProductPropertyItem(item)));
					}else if(pType == Type_ProductProperty.Rang){
						properyItemFilterQueryGroup.or(QRoot.price.between(pItem.getMin(), pItem.getMax()));
					}
				}
				properyItemFilterQuery.and(properyItemFilterQueryGroup);
			}
		}
//		if(null != filter){
////			for(Long item : filter){
////				TB_ProductPropertyItem pItem = service_ProductPropertyItem.findById(item);
////				Type_ProductProperty pType = pItem.getProductProperty().getType();
////				if(pType == Type_ProductProperty.Fix){
////					properyItemFilterQuery.and(QRoot.productPropertyItems.contains(new TB_ProductPropertyItem(item)));
////				}else if(pType == Type_ProductProperty.Rang){
////					properyItemFilterQuery.and(QRoot.price.between(pItem.getMin(), pItem.getMax()));
////				}
////			}
//		}
		// 过滤参数
		BooleanBuilder nameFilterQuery = new BooleanBuilder();
		if(null != name){
			nameFilterQuery.and(QRoot.name.like("%" + name + "%"));
		}
		// 
		List<TB_Product> list = service_Product.list(
				it->it.state.eq(State_Product.Uped)
					.and(properyItemFilterQuery)
					.and(nameFilterQuery)
					.and(typeQuery)
					.and(wineQuery), 
				it->new QSort(it.orderValue.desc())
			).getContent();
		//
		return new RestDataOutputFormat(
				list
				.stream()
				.map(it->{
					return new Output(it, totalScore, townId, service_ProductPropertyItem);
				})
				.collect(Collectors.toList()), 
				null == category ? null : category.getId()
			);
	}
	
	public static class Input {
		private @Getter @Setter List<List<Long>> filters = new ArrayList<>();
	}

}
